package com.lifesea.notify.push;

import com.lifesea.base.entity.PushOsTypeEnums;
import com.lifesea.commons.exception.ErrorCodeConst;
import com.lifesea.commons.exception.ErrorCodeException;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Push工厂类，支持Android和IOS
 * User: chendw
 * Date: 2016/7/19 12:39
 * To change this template use File | Settings | File Templates.
 */
@Component
public class PushFactory implements ApplicationContextAware {

    private static AndroidPushService androidPushService;

    private static IosPushService iosPushService;

    public static IPushService get(Integer osType) {
        IPushService pushService;
        if (osType == PushOsTypeEnums.ANDROID.value()) {
            pushService = androidPushService;
        } else if (osType == PushOsTypeEnums.IOS.value()) {
            pushService = iosPushService;
        } else {
            throw new ErrorCodeException(ErrorCodeConst.GLOBAL_REQUEST_ERROR, String.format("未定义的OsType类型[%s]", osType));
        }

        return pushService;
    }

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        androidPushService = context.getBean(AndroidPushService.class);
        iosPushService = context.getBean(IosPushService.class);
    }
}
